//
//  NSMutableDictionary+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSMutableDictionary+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation NSMutableDictionary (THHook)

+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls = NSClassFromString(@"__NSDictionaryM");
        ExchangeImplementationsInTwoClasses(cls,@selector(setValue:forKey:), [NSMutableDictionary class], @selector(avoidCrashSetObject:forKey:));
        ExchangeImplementationsInTwoClasses(cls, @selector(removeObjectForKey:), [NSMutableDictionary class], @selector(avoidCrashRemoveObjectForKey:));
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0){
             ExchangeImplementationsInTwoClasses(cls,@selector(setObject:forKeyedSubscript:),[NSMutableDictionary class],@selector(avoidCrashSetObject:forKeyedSubscript:));
        }
    });
}
- (void)avoidCrashSetObject:(id)anObject forKey:(id<NSCopying>)aKey {
    @try {
        [self avoidCrashSetObject:anObject forKey:aKey];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        
    }
}
#pragma mark - removeObjectForKey:
- (void)avoidCrashRemoveObjectForKey:(id)aKey {
    @try {
        [self avoidCrashRemoveObjectForKey:aKey];
    }@catch (NSException *exception) {
         [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        
    }
}
#pragma mark - setObject:forKeyedSubscript:
- (void)avoidCrashSetObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
    @try {
        [self avoidCrashSetObject:obj forKeyedSubscript:key];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        
    }
}
@end
